﻿using BoardBroker;
using ComLib.ComType;
using CoreBroker;
using KWindow.KPageCore;
using LogLib;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace IPCPages
{
    public delegate void TakePhotoCountdownUpdatedHandler(int countDownSec);
    public class ProcessTakePhoto : PageProcessBase<ProcessTakePhoto>
    {
        /// <summary>
        /// 拍照业务控制对象
        /// </summary>
        public Taker Taker  { private set; get; }

        /// <summary>
        /// 拍照选项
        /// </summary>
        public TakePhotoOption Option { private set; get; }

        private CoreBroker.CapturerBroker capturerBroker1;

        private BoardControl _board = BoardControl.Instance;

        private ProcessTakePhoto() : base()
        {
            InitOption();
        }

        private void InitOption()
        {
            Option = new TakePhotoOption();
            if (!File.Exists(Option.ConfigFilePath))
            {
                Option.Save();
            }
            Option.Load();
            Log.WriteAsync($"拍照选项:{Option}");
        }

        public event TakePhotoCountdownUpdatedHandler CountdownUpdated;
        protected void OnCountdownUpdated(int sec)
        {
            if (CountdownUpdated != null)
            {
                CountdownUpdated(sec);
            }
        }

        public override void Init()
        {
            base.Init();
        }

        public override void Exit()
        {
            base.Exit();
        }

        public override void InitData()
        {
            capturerBroker1 = GetValue<CoreBroker.CapturerBroker>("Capturer");
            if (capturerBroker1 == null)
            {
                Log.WriteException("获取相机失败!");
                return;
            }
            Taker = new Taker(capturerBroker1);
            // 设置照片应用业务类型
            Taker.App = GetValue<ApplyTo>("ApplyTo");
            SetValue("Taker", Taker);
            base.InitData();
            int actionIndex = GetValue<int>("ActionIndex");
            Log.WriteAsync($"用户进入第{actionIndex}次拍照尝试...");
        }

        public override void UpdateData()
        {
            base.UpdateData();
        }

        public override void StartWork()
        {
            base.StartWork();
            Taker.StartWork();
            MainPage.Speak("准备就绪，将连拍两张");
            Task.Factory.StartNew(() => OnTaking());
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void PlayWelcomeVoice()
        {
            base.PlayWelcomeVoice();
        }

        private void OnTaking()
        {
            int interval = Option.CountdownInterval;

            Log.WriteAsync("拍照开始...");
            MainPage.Speak("现在拍摄第一张，请注视前方");
            Thread.Sleep(3000);

            for (int i = 3; i > 0; i--)
            {
                OnCountdownUpdated(i);
                Thread.Sleep(interval);
            }

            Taker.TakeOne(0);
            Thread.Sleep(1000);
            MainPage.Speak("第一张拍好了，现在准备拍第二张");

            Thread.Sleep(Option.TakingInterval);

            MainPage.Speak("现在拍摄第二张");

            for (int i = 3; i > 0; i--)
            {
                OnCountdownUpdated(i);
                Thread.Sleep(interval);
            }

            Taker.TakeOne(1);
            Thread.Sleep(2000);
            MainPage.Speak("第二张拍好了，现在请放松，等待检测完成");

            _board.StopWorking();
            capturerBroker1.StopCapturer();

            OnPhotoDetecting();
            Log.WriteAsync("拍照结束...");
        }

        private void OnPhotoDetecting()
        {
            if (MainPage.InvokeRequired)
            {
                Action outdelegate = new Action(OnPhotoDetecting);
                MainPage.Invoke(outdelegate);
                return;
            }
            if(IsRunning)
            {
                ToAny("IPCPages.PageDetectPhoto");
            }
            else
            {
                Log.WriteAsync("意外的错误: 不执行页面跳转.");
            }
        }
    }
}
